package com.gongpingjialoan.imageload;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;

public class BitmapCache implements ImageCache {

	static  BitmapCache mBitmapCache;
	LruCache<String, Bitmap> lruCache;



	public static BitmapCache getInstance(){
		if(mBitmapCache == null){
			mBitmapCache = new BitmapCache();
		}
		return  mBitmapCache;
	}


	public BitmapCache() {
		int maxMemory = (int) Runtime.getRuntime().maxMemory();
		int cacheSize = maxMemory / 8;
		lruCache = new LruCache<String, Bitmap>(cacheSize){
			@Override
			protected int sizeOf(String key, Bitmap bitmap){
				return bitmap.getRowBytes() * bitmap.getHeight();
			}
		};
	}
	@Override
	public Bitmap getBitmap(String arg0) {
		return lruCache.get(arg0);
	}

	@Override
	public void putBitmap(String arg0, Bitmap arg1) {
		if(getBitmap(arg0) == null){
			lruCache.put(arg0, arg1);
		}
	}
}
